﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using System.Web.Services;
using System.Web.Script.Services;
namespace FilStartUps.Forum
{
    public partial class ThreadReply : System.Web.UI.Page
    {
        TextBox txtmsgs = new TextBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetForumMsg();
            }
        }

        private void GetForumMsg()
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                int forumthreadid = int.Parse(Request.QueryString["id"].Trim());

                var threadtitle = db_connect.ForumThreads.Where(f => f.forumthreadidPK.Equals(forumthreadid)).SingleOrDefault();
                lbthreadtitle.Text = threadtitle.threadtitle;

                var forummsg = (from fm in db_connect.ForumMessages
                                join m in db_connect.Members
                                on fm.memberidFK equals m.memberidPK
                                where fm.forumthreadidFK.Equals(forumthreadid)
                                && fm.disabled.Equals(false)
                                select new
                                {
                                    message = fm.message,
                                    dateposted = fm.dateposted,
                                    memberidFK = fm.memberidFK,
                                    emailaddress = m.emailaddress
                                }).ToList();
                rpMessage.DataSource = forummsg;
                rpMessage.DataBind();
            }
        }


        protected void btnreply_Click(object sender, EventArgs e)
        {
            if (txtmsg.Text == string.Empty)
            {
                //Validation
            }
            else
            {
                using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
                {
                    ForumMessage msg = new ForumMessage();
                    var user = db_connect.Members.Where(m => m.emailaddress.Equals(User.Identity.Name)).SingleOrDefault();
                    msg.dateposted = DateTime.Now;
                    msg.disabled = false;
                    msg.forumthreadidFK = Convert.ToInt32(Request.QueryString["id"].Trim());
                    msg.memberidFK = user.memberidPK;
                    msg.message = txtmsg.Text.Trim();
                    db_connect.ForumMessages.Add(msg);
                    db_connect.SaveChanges();

                    Response.Redirect("~/Forum/ShowMessage.aspx?t=" + msg.forumthreadidFK);
                }
            }
        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Forum/ShowMessage.aspx?t=" + Request.QueryString["id"].Trim());
        }

    }
}